package org.oos.dao.impl;

import static org.oos.ApiUrlProvider.SiteCheckin;
import static org.oos.dao.options.OptionsMap.FINDERS.CHECKIN_OPTIONS;
import static org.oos.dao.options.OptionsMap.FINDERS.*;

import org.oos.atom.CheckinFeed;
import org.oos.atom.entries.CheckinEntry;
import org.oos.atom.entries.impl.CheckinEntryImpl;
import org.oos.atom.impl.CheckinFeedImpl;
import org.oos.dao.SiteCheckinDao;
import org.oos.dao.exceptions.ReadException;
import org.oos.dao.lists.impl.ReadOnlyDynamicList;
import org.oos.dao.options.CheckinOptions;
import org.oos.dao.options.FieldOptions;
import org.oos.dao.options.PageOptions;
import org.oos.dao.options.CheckinOptions.CheckinRequestType;
import org.oos.lists.DynamicList;

import com.google.api.client.http.GenericUrl;

/**
 * 
 * Checkin DAO
 * 
 * @author pablo.gonzalez@11870.com
 * 
 * @see SiteCheckinDao
 * 
 */
public class SiteCheckinDaoImpl extends
		AbstractDao<CheckinEntry, CheckinFeed<CheckinEntry>> implements
		SiteCheckinDao {
	
	private static final long serialVersionUID = -2195334066203552863L;
	private String siteSlug;

	public SiteCheckinDaoImpl() {
		super(CheckinFeedImpl.class, CheckinEntryImpl.class);
	}

	@Override
	public GenericUrl getFeedUrl() {
		return SiteCheckin.getUrl(siteSlug);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected ReadOnlyDynamicList<CheckinEntry, CheckinFeed<CheckinEntry>> getList(CheckinFeed feed) {
		return new ReadOnlyDynamicList<CheckinEntry, CheckinFeed<CheckinEntry>>(this, feed);
	}
	
	@Override
	public void setSiteSlug(String userSlug) {
		this.siteSlug = userSlug;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public DynamicList<CheckinEntry> read() throws ReadException {
		FieldOptions options = FIELD_OPTIONS.get(getOptions());
		options.clear();
		options.add("author");
		options.add("updated");
		options.add("oos:count");
		options.add("content");
		CheckinOptions checkinOptions = CHECKIN_OPTIONS.get(getOptions());
		checkinOptions.setType(CheckinRequestType.BOTH);
		return super.read();
	}
	
	@Override
	public int getRealSize() throws ReadException {
		FieldOptions options = FIELD_OPTIONS.get(getOptions());
		options.clear();
		options.add("");

		PageOptions pageOptions = PAGE_OPTIONS.get(getOptions());
		pageOptions.setPageSize(1);
		CheckinFeed<?> feed = super.readFeed(1);
		
		return feed.getTotalResults();
	}

}
